home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / ldexp.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  59 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "pml.h"
  4.  
  5. /*
  6.  * ldexp returns the quanity "value" * 2 ^ "exp"
  7.  * 
  8.  * For the mc68000 using IEEE format the double precision word format is:
  9.  * 
  10.  * WORD N   =>    SEEEEEEEEEEEMMMM 
  11.  * WORD N+1 =>    MMMMMMMMMMMMMMMM 
  12.  * WORD N+2 =>    MMMMMMMMMMMMMMMM 
  13.  * WORD N+3 =>    MMMMMMMMMMMMMMMM
  14.  * 
  15.  * Where:          S  =>   Sign bit 
  16.  *                 E  =>   Exponent 
  17.  *                 X  =>   Ignored (set to 0)
  18.  *                 M  =>   Mantissa bit
  19.  * 
  20.  * NOTE:  Beware of 0.0; on some machines which use excess 128 notation for the
  21.  * exponent, if the mantissa is zero the exponent is also.
  22.  * 
  23.  */
  24.  
  25. #define MANT_MASK 0x800FFFFF    /* Mantissa extraction mask     */
  26. #define ZPOS_MASK 0x3FF00000    /* Positive # mask for exp = 0  */
  27. #define ZNEG_MASK 0x3FF00000    /* Negative # mask for exp = 0  */
  28.  
  29. #define EXP_MASK 0x7FF00000 /* Mask for exponent            */
  30. #define EXP_SHIFTS 20       /* Shifts to get into LSB's     */
  31. #define EXP_BIAS 1023       /* Exponent bias                */
  32.  
  33.  
  34. union dtol {
  35.     double          dval;
  36.     int             ival[2];
  37. };
  38.  
  39. double
  40. ldexp(value, exp)
  41.     double          value;
  42.     int             exp;
  43. {
  44.     union dtol      number;
  45.     int            *iptr, cexp;
  46.  
  47.     if (value == 0.0)
  48.         return (0.0);
  49.     else {
  50.         number.dval = value;
  51.         iptr = &number.ival[0];
  52.         cexp = (((*iptr) & EXP_MASK) >> EXP_SHIFTS) - EXP_BIAS;
  53.         *iptr &= ~EXP_MASK;
  54.         exp += EXP_BIAS;
  55.         *iptr |= ((exp + cexp) << EXP_SHIFTS) & EXP_MASK;
  56.         return (number.dval);
  57.     }
  58. }
  59.